package com.stx.openeyes;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.stx.openeyes.service.DownloadService;
import com.stx.openeyes.utils.ContextUtil;
import com.stx.openeyes.utils.NToast;
import com.stx.openeyes.utils.ToastUtil;
import com.stx.openeyes.view.CustomTextView;
import com.stx.openeyes.view.activity.FunctionActivity;
import com.stx.openeyes.view.activity.SearchActivity;
import com.stx.openeyes.view.fragment.DailyFragment;
import com.stx.openeyes.view.fragment.FindFragment;
import com.stx.openeyes.view.fragment.HotFragment;
import com.stx.openeyes.view.fragment.MyFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 主界面
 */
public class MainActivity extends AppCompatActivity {
    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.main_toolbar_tv_time)
    CustomTextView mainToolbarTvTime;

    @BindView(R.id.tv_daily)
    LinearLayout tvDaily;
    @BindView(R.id.tv_daily_iv)
    ImageView tvDailyIv;
    @BindView(R.id.tv_daily_tv)
    TextView tvDailyTv;
    @BindView(R.id.tv_my)
    LinearLayout tvMy;
    @BindView(R.id.tv_my_iv)
    ImageView tvMyIv;
    @BindView(R.id.tv_my_tv)
    TextView tvMyTv;

    private FragmentTransaction transaction;
    private HotFragment hotFragment;
    private MyFragment myFragment;
    private FragmentManager fragmentManager;
    private long mExitTime = 0;
    private int currentItemIndex = 0;

    private static Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        setContentView(R.layout.activity_main);
        StatusBarUtil.setTranslucent(this);
        //获取控件
        ButterKnife.bind(this);
        //获取Fragment的管理器
        fragmentManager = getSupportFragmentManager();
        initView();

        // 启动下载监听器
        startService(new Intent(this, DownloadService.class));
    }

    //初始化界面
    private void initView() {
        setSupportActionBar(mainToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);//设置不显示标题

        //设置默认第一个菜单按钮为选中状态
        setChocie(1);
    }

    /**
     * 底部菜单栏的点击切换
     * @param currenItem
     */
    private void setChocie(int currenItem) {
        currentItemIndex = currenItem;
        transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        clearChioce();
        switch (currenItem) {
            case 1://热门排行
                mainToolbar.setVisibility(View.VISIBLE);
                tvDailyIv.setImageResource(R.drawable.main_home_checked);
                tvDailyTv.setTextColor(getResources().getColor(R.color.colorBlack));
                tvDailyTv.setTextSize(13);
                if (hotFragment == null) {
                    hotFragment = new HotFragment();
                    transaction.add(R.id.main_ll_fragment, hotFragment);
                } else {
                    transaction.show(hotFragment);
                }
                break;
            case 3://我的
                mainToolbar.setVisibility(View.GONE);
                tvMyIv.setImageResource(R.drawable.main_user_checked);
                tvMyTv.setTextColor(getResources().getColor(R.color.colorBlack));
                tvMyTv.setTextSize(13);
                if (myFragment == null) {
                    myFragment = new MyFragment();
                    transaction.add(R.id.main_ll_fragment, myFragment);
                } else {
                    transaction.show(myFragment);
                }
                break;
            default:
                break;
        }
        //提交事务
        transaction.commit();
    }

    /**
     * 隐藏所有的Fragment，避免fragment混乱
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (myFragment != null) {
            transaction.hide(myFragment);
        }
        if (hotFragment != null) {
            transaction.hide(hotFragment);
        }
    }

    /**
     * 重置所有选项
     */
    private void clearChioce() {
        //还原默认选项
        tvDailyIv.setImageResource(R.drawable.main_home);
        tvDailyTv.setTextColor(getResources().getColor(R.color.colorGray));
        tvDailyTv.setTextSize(11);
        tvMyIv.setImageResource(R.drawable.main_user);
        tvMyTv.setTextColor(getResources().getColor(R.color.colorGray));
        tvMyTv.setTextSize(11);
    }

    // 按两次退出程序
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                ToastUtil.showToast(MainActivity.this, "再按一次退出程序");
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.tv_daily, R.id.tv_my, R.id.et_searchtext_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_daily:
                setChocie(1);
                break;
            case R.id.tv_my:
                setChocie(3);
                break;
            case R.id.et_searchtext_search:
                startActivity(new Intent(getContext(), SearchActivity.class));
                break;
        }
    }

    public static Context getContext() {
        return context;
    }

}
